﻿/*
·--------------------------------------------------------------------·
| TranslatorAddin - TranslatorViewModel                              |
| Copyright (c) 2010, Ben Chege.                                     |
| http://code.google.com/p/translatoraddin                           |
·--------------------------------------------------------------------·
| This software is free software; you can redistribute it and/or     |
| modify it under the terms of the GNU Lesser General Public         |
| License as published by the Free Software Foundation; either       |
| version 2.1 of the License, or (at your option) any later version. |
|                                                                    |
| This software is distributed in the hope that it will be useful,   |
| but WITHOUT ANY WARRANTY; without even the implied warranty of     |
| MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU  |
| Lesser General Public License for more details.                    |
|                                                                    |
| GNU LGPL: http://www.gnu.org/copyleft/lesser.html                  |
·--------------------------------------------------------------------·
*/

namespace TranslatorAddin.TaskPane
{
   using System.Collections.ObjectModel;
   using MvvmFoundation.Wpf;
   using Google.API.Translate;
   using System.Collections.Generic;
    using System;
    using System.Runtime.Remoting.Messaging;
    using System.ComponentModel;

   public class TranslatorViewModel : ObservableObject
   {
      private ObservableCollection<Language> fromLanguages;
      private ObservableCollection<Language> toLanguages;
      private Language selectedFromLanguage;
      private Language selectedToLanguage;
      private RelayCommand translate;
      private RelayCommand detect;

      public TranslatorViewModel(ICollection<Language> languages)
      {
         this.fromLanguages = new ObservableCollection<Language>();
         this.toLanguages = new ObservableCollection<Language>();

         foreach (var language in languages)
         {
            this.fromLanguages.Add(language);
            this.toLanguages.Add(language);
         }
      }

      public Language SelectedFromLanguage
      {
         get
         {
            return this.selectedFromLanguage;
         }
         set
         {
            if (this.selectedFromLanguage != value)
            {
               this.selectedFromLanguage = value;
               base.RaisePropertyChanged("SelectedFromLanguage");
            }
         }
      }

      public Language SelectedToLanguage
      {
         get
         {
            return this.selectedToLanguage;
         }
         set
         {
            if (this.selectedToLanguage != value)
            {
               this.selectedToLanguage = value;
               base.RaisePropertyChanged("SelectedToLanguage");
            }
         }
      }

      public ObservableCollection<Language> FromLanguages
      {
         get
         {
            return this.fromLanguages;
         }
      }

      public ObservableCollection<Language> ToLanguages
      {
         get
         {
            return this.toLanguages;
         }
      }

      public RelayCommand Translate
      {
         get
         {
            if (this.translate == null)
            {
               this.translate = new RelayCommand(TranslateExecute, CanTranslate);
            }

            return this.translate;
         }
      }
      public RelayCommand Detect
      {
          get
          {
              if (this.detect == null)
              {
                  this.detect = new RelayCommand(DetectFromLanguage, CanDetect);
              }

              return this.detect;
          }
      }
      public void DetectFromLanguage()
      {

          var langdetectworker = new DocumentTranslator.DetectLanguageDelegate(DocumentTranslator.DetectLanguage);
          langdetectworker.BeginInvoke(delegate(IAsyncResult ar)
          {
              //SelectedFromLanguage = DocumentTranslator.DetectLanguage();
              DocumentTranslator.DetectLanguageDelegate worker =
                                    (DocumentTranslator.DetectLanguageDelegate)((AsyncResult)ar).AsyncDelegate;
              AsyncOperation async = (AsyncOperation)ar.AsyncState;

              // finish the asynchronous operation
              SelectedFromLanguage = (Language)worker.EndInvoke(ar);
          },null);
          
      }
      private void TranslateExecute()
      {
         DocumentTranslator.TranslateDocument(
                     this.SelectedFromLanguage, 
                                    this.SelectedToLanguage);
      }

      private bool CanDetect()
      {
          return true;
      }
      private bool CanTranslate()
      {
         if (this.SelectedFromLanguage == null)
         {
            return false;
         }

         if (this.SelectedToLanguage == null)
         {
            return false;
         }

         if (this.SelectedFromLanguage == this.SelectedToLanguage)
         {
            return false;
         }

         return true;
      }
   }
}
